package com.blog.controller;

import com.blog.pojo.User;
import com.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

//    @GetMapping("/login")
//    public String loginPage() { return "login"; }

    @GetMapping("/")
    public String loginPage() { return "login"; }

    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        HttpSession session,
                        RedirectAttributes attributes) {
        if (username.trim().isEmpty() && password.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "用户名和密码不能为空");
            return "redirect:/";
        } else if (username.trim().isEmpty() || password.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "用户名或密码不能为空");
            return "redirect:/";
        }

        User user = userService.checkUser(username, password);

        if (user != null) {
            user.setPassword(null);
            // 假设登录成功，将用户信息存储在session中
//            User loggedInUser = userService.authenticateUser(username, password);
            session.setAttribute("user", user);

            return "redirect:/index";
        } else {
            attributes.addFlashAttribute("message", "用户名或密码错误");
            return "redirect:/";
        }
    }


    @PostMapping("/register")
    public String register(@RequestParam("username") String username,
                           @RequestParam("email") String email,
                           @RequestParam("password") String password,
                           @RequestParam("confirmPassword") String confirmPassword,
                           HttpSession session,
                           RedirectAttributes attributes) {
        // 判断用户名是否为空
        if (username.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "用户名不能为空");
            return "redirect:/";
        }

        // 判断邮箱是否为空
        if (email.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "邮箱不能为空");
            return "redirect:/";
        }

        // 判断密码是否为空
        if (password.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "密码不能为空");
            return "redirect:/";
        }

        // 判断确认密码是否为空
        if (confirmPassword.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "确认密码不能为空");
            return "redirect:/";
        }

        // 判断用户名是否已存在
        if (userService.isUserExists(username)) {
            attributes.addFlashAttribute("message", "用户名已被注册");
            return "redirect:/";
        }

        // 判断邮箱是否已存在
        if (userService.isEmailExists(email)) {
            attributes.addFlashAttribute("message", "邮箱已被注册");
            return "redirect:/";
        }

        // 判断两次密码是否一致
        if (!password.equals(confirmPassword)) {
            attributes.addFlashAttribute("message", "输入的两次密码不一致");
            return "redirect:/";
        }

        // 注册用户
        userService.registerUser(username, email, password);
        return "redirect:/";
    }


}
